package com.eas.biz.service.user.impl;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eas.biz.framework.cache.CacheConfig;
import com.eas.biz.framework.cache.CacheProvider;
import com.eas.biz.framework.cache.CacheStore;
import com.eas.biz.service.user.domain.SchoolInfo;
import com.eas.biz.service.user.interfaces.SchoolService;

@Service("schoolCacheService")
public class SchoolCacheServiceImpl extends SchoolServiceImpl implements SchoolService {

    @Autowired
    private CacheProvider cacheProvider;

    private CacheStore cacheStore;

    @PostConstruct
    public void init() {
        CacheConfig config = new CacheConfig();
        config.setName("schoolInfo");
        config.setRefreshSeconds(24 * 60 * 60);
        config.setCapacity(50);
        cacheStore = cacheProvider.create(config);
    }

    @Override
    public SchoolInfo getSchoolInfo(long id) {
        Object result = cacheStore.get(id);
        if (result != null && result instanceof SchoolInfo) {
            return (SchoolInfo) result;
        } else {
            SchoolInfo schoolInfo = super.getSchoolInfo(id);
            if (schoolInfo != null) {
                cacheStore.put(id, schoolInfo);
            }
            return schoolInfo;
        }
    }
}
